// 表格相关工具函数
import { VxeTablePropTypes } from "vxe-table"
import bignumber from "bignumber.js"

/**
 * 计算表格列数据总和
 * @param list 数据列表
 * @param field 字段名
 * @returns 总和数值
 */
export const sumNum = (list: any, field: string) => {
  let count = new bignumber(0)
  list.forEach((item: any) => {
    // 创建 bignumber 对象并进行累加
    const valueToAdd = new bignumber(item[field] || 0)
    count = count.plus(valueToAdd)
  })
  return count.toNumber()
}

/**
 * 表格底部统计方法
 * @param param0 表格列和数据
 * @returns 统计行数据
 */
export const footerMethod: VxeTablePropTypes.FooterMethod<any> = ({ columns, data }) => {
  return [
    columns.map((column, columnIndex) => {
      if (columnIndex === 0) {
        return "统计"
      }
      if (["billAmount", "transaction_amount", "out_discount", "out_finally"].includes(column.field)) {
        return sumNum(data, column.field)
      }

      return null
    })
  ]
}
